package com.lantosec.hmrp.modules.eval.controller.app;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.log.ControllerLog;
import com.lantosec.hmrp.core.log.LogType;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.modules.eval.entity.EvalEventExtraduty;
import com.lantosec.hmrp.modules.eval.service.IEvalEventExtradutyService;
import com.lantosec.hmrp.modules.eval.vo.EvalEventExtradutyVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 *
 * 考评任务事项追加责任 控制器
 *
 * @author AutoCode
 * @since 2020-03-19
 */
@Api(value = "考评任务事项追加责任控制器APP端", tags = "考评任务事项追加责任控制器APP端")
@RestController
@RequestMapping("/eval/app/event/extraduty")
@Log4j2
public class EvalEventExtradutyAppController extends BaseController {
    @Autowired
    private IEvalEventExtradutyService service;

    @GetMapping("all/{evalTaskEventId}")
    @ApiOperation(value = "查询事件下的全部责任")
    public Result<List<EvalEventExtradutyVO>> all(@ApiParam(value = "考评事件id", required = true) @PathVariable(name="evalTaskEventId") Integer evalTaskEventId){
        List<EvalEventExtraduty> evalEventExtradutyList = service.list(new QueryWrapper<EvalEventExtraduty>().eq(EvalEventExtraduty.ColName.eval_task_event_id.toString(),evalTaskEventId));
        return Result.ok(entity2VO(evalEventExtradutyList));
    }

    @PostMapping("create")
    @ControllerLog(name = "新增追加责任", type = LogType.add)
    @ApiOperation(value = "新增追加责任")
    public Result<Integer> create(@ApiParam(value = "考评任务事项追加责任VO对象", required = true) @RequestBody @Validated EvalEventExtradutyVO vo, BindingResult bindingResult){
        validationHandler(bindingResult);
        EvalEventExtraduty entity = vo.convertToEntity();
        service.save(entity);
        return Result.ok(entity.getEvalEventExtradutyId());
    }

    @PutMapping("update")
    @ControllerLog(name = "修改追加责任", type = LogType.update)
    @ApiOperation(value = "修改数据")
    public Result<Void> update(@ApiParam(value = "考评任务事项追加责任VO对象", required = true) @RequestBody EvalEventExtradutyVO vo){
        EvalEventExtraduty entity = vo.convertToEntity();
        service.updateById(entity);
        return Result.ok();
    }

    @DeleteMapping(value = "delete/{ids}")
    @ControllerLog(name = "逻辑删除追加责任", type = LogType.delete)
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }
}
